package com.example.yangjintingtest.controller;


import com.example.yangjintingtest.domain.User;
import com.example.yangjintingtest.mapper.UserMapper;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


@Controller
public class loginController {
    @Autowired
    UserMapper userMapper;

    @GetMapping("/login")
    public String toLoginPage() {
        return "client/login";
    }

    //登录
    @PostMapping("/toLogin")
    public String login(User user, HttpSession httpSession, Model model) {
        User tUser = userMapper.userLogin(user);
        if (tUser!=null) {
            // 将用户对象添加到Session
            httpSession.setAttribute("user", tUser);
            model.addAttribute("user", tUser);
            // 跳转到主页
//            return  "client/index";
              return "redirect:/";
        }
        else {
            model.addAttribute("msg", "账号或密码错误，请重新输入！");
            return "client/login";
        }
    }


}